<?php

namespace app\api\controller\worker;

use addons\epay\library\Service;
use app\admin\model\Article;
use app\admin\model\Articletype;
use app\admin\model\City;
use app\admin\model\Cmdlog;
use app\admin\model\Devicelock;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shopjoin;
use app\admin\model\Shoporder;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shoptuan;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Usertuan;
use app\admin\model\Userweixin;
use app\api\service\DeviceService;
use app\api\service\LockService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;
use Yansongda\Pay\Pay;

/**
 */
class Cmd extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }


    /**
     * 打扫开门
     */
    public function openDoor()
    {

        $user = $this->auth->getUser();
        $type = $this->request->param('type');
        $shop_id = $this->request->param('shop_id');
        $room_id = $this->request->param('room_id');
        $id = $this->request->param('id');
        if ($room_id != "") {

            $roomModel = new Shoproom();
            $room = $roomModel->where('id', $room_id)->find();
            $shop_id = $room['shop_id'];
        }

        $deviceModel = new Shopdevice();
        if ($type == 'door') {
            $device = $deviceModel->where('shop_id', $shop_id)->where('room_id', -1)->find();

            error_log("===device===" . json_encode($device));
            $sn = $device['sn'];
            $req = array();
            $req['sec'] = 10;
            $req['orderNo'] = getOrderSn();
            $req['sn'] = $sn;
            $req['voice'] = "";

            $cmdModel = new Cmdlog();
            if ($device['prod'] == 1) {

                $ret = http_post_json($this->openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                } else {
                    $msg = "发送指令失败";

                }
            } else {
                $deviceService = new DeviceService();

                $doorReq = array();
                $doorReq['open'] = 1;
                $doorReq['open2'] = 1;
                $doorReq['open3'] = 1;
                $doorReq['open4'] = 1;
                $doorReq['device_sn'] = $device['device_sn'];

                $timeReq = array();
                $timeReq['device_sn'] = $device['device_sn'];
                $timeReq['sec'] = 0;
                $timeReq['time'] = time();
                $deviceService->dxIotDoor($doorReq);
                $deviceService->dxIotTime($timeReq);
            }

            $cmdModel->insert(['user_id' => $user['id'], 'cmd' => json_encode($req), 'create_time' => time()]);
            $this->success($msg);

        } else {

            $cleanModel = new Roomclean();
            $clean = $cleanModel->where('id', $id)->find();
            if (!$clean) $this->error("保洁数据不存在");
            $orderModel = new Shoporder();
            $order = $orderModel->where('room_id', $clean['room_id'])->where('status', 'doing')->find();
            if ($order) {
                $this->error("此包厢已有人在使用，暂不需保洁");
            }
            $cleanModel->where('id', $id)->update(['clean_start_time' => time(), 'order_no' => getOrderSn(), 'status' => 'doing']);

            $device = $deviceModel->where('shop_id', $clean['shop_id'])->where('room_id', $clean['room_id'])->find();

            //发送指令
            $sn = $device['sn'];
            $order_no = getOrderSn();
            $req = array();
            $req['sec'] = 30 * 60;
            $req['orderNo'] = getOrderSn();
            $req['sn'] = $sn;
            $req['voice'] = "";

            if ($device['prod'] == 1) {

                if ($device['version'] == 1) {
                    $ret = http_post_json($this->openCmdUrl, json_encode($req));
                    $ret = json_decode($ret, true);
                    if ($ret['code'] == 0) {
                        $msg = "正在打开中";
                    } else {
                        $msg = "发送指令失败";

                    }

                    $deviceLockModel = new Devicelock();
                    $lock = $deviceLockModel->where('room_id', $device['room_id'])->find();


                    if ($lock) {
                        if ($lock['is_gateway'] == 1) {
                            //网关开锁
                            $lockService = new LockService();
                            $lockReq = array();
                            $lockReq['lockId'] = $lock['lockId'];
                            $lockService->openLock($lockReq);
                        }
                    }

                } else {
                    $req = array();
                    $req['sec'] = 30 * 60;
                    $req['orderId'] = 1;
                    $req['deviceSn'] = $device['device_sn'];
                    $ret = http_post_json($this->openV3CmdUrl, json_encode($req));
                    $ret = json_decode($ret, true);
                    if ($ret['code'] == 0) {
                        $msg = "正在打开中";
                    }
                }
            } else {
                $deviceService = new DeviceService();

                $doorReq = array();
                $doorReq['open'] = 1;
                $doorReq['open2'] = 1;
                $doorReq['open3'] = 1;
                $doorReq['open4'] = 1;
                $doorReq['device_sn'] = $device['device_sn'];

                $timeReq = array();
                $timeReq['device_sn'] = $device['device_sn'];
                $timeReq['sec'] = 30 * 60;
                $timeReq['time'] = time();
                $deviceService->dxIotDoor($doorReq);
                $deviceService->dxIotTime($timeReq);
            }

            $cmdModel = new Cmdlog();

            $cmdModel->insert(['user_id' => $user['id'], 'cmd' => json_encode($req),
                'create_time' => time(), 'order_no' => $order_no, 'shop_id' => $clean['shop_id'], 'room_id' => $clean['room_id']]);


        }

        $ret = array();
        $ret['is_bt'] = $device['is_bt'];
        $ret['room_id'] = $device['room_id'];
        $ret['shop_id'] = $device['shop_id'];

        $deviceLockModel = new Devicelock();
        $lock = $deviceLockModel->where('room_id', $device['room_id'])->find();

        error_log("===work=clean=" . json_encode($lock));
        if ($lock) {
            if ($lock['is_gateway'] == 1) {
                //网关开锁
                $lockService = new LockService();
                $lockReq = array();
                $lockReq['lockId'] = $lock['lockId'];
                $lockService->openLock($lockReq);

            }
        }

        $this->success('指令发送成功', $ret);
    }


    public function done()
    {

        $id = $this->request->param('id');
        $room_id = $this->request->param('room_id');

        $cleanModel = new Roomclean();
        $clean = $cleanModel->where('id', $id)->find();
        if (!$clean) {
            $clean = $cleanModel->where('room_id', $room_id)->where('status', 'doing')->find();
        }

        if (!$room_id) {
            $room_id = $clean['room_id'];
        }


//        if($clean['start_time'] > time()){
//
//            $this->error("订单还未结束 不可点击完成打扫");
//        }

//        if($clean['clean_start_time'] == -1){
//            $this->error("请先点击开始打扫");
//
//        }
        $cleanModel->where('id', $id)->update(['clean_end_time' => time(), 'status' => 'done']);

        $deviceModel = new Shopdevice();
        $device = $deviceModel->where('shop_id', $clean['shop_id'])->where('room_id', $clean['room_id'])->find();
        $user = $this->auth->getUser();
        $sn = $device['sn'];
        $order_no = $clean['order_no'];
        $req = array();
        $req['orderNo'] = $order_no;
        $req['sn'] = $sn;
        $req['sec'] = 0;
        $req['voice'] = "";

        $cmdModel = new Cmdlog();
        $shopRoomModel = new Shoproom();
        $shopRoomModel->where('id', $room_id)->update(['is_clean' => 1, 'status' => 'free']);

        $orderModel = new Shoporder();
        $orderModel->where('order_no', $order_no)->update(['status' => 'done']);

        if ($device['prod'] == 1) {
            if ($device['version'] == 1) {
                $ret = http_post_json($this->openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                } else {
                    $msg = "发送指令失败";

                }
            } else {
                $req = array();
                $req['sec'] = 0;
                $req['orderId'] = 1;
                $req['deviceSn'] = $device['device_sn'];
                $ret = http_post_json($this->openV3CmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }
            }
        } else {
            $deviceService = new DeviceService();
            $doorReq = array();
            $doorReq['open'] = 0;
            $doorReq['open2'] = 0;
            $doorReq['open3'] = 0;
            $doorReq['open4'] = 0;
            $doorReq['device_sn'] = $device['device_sn'];

            $timeReq = array();
            $timeReq['device_sn'] = $device['device_sn'];
            $timeReq['sec'] = 0;
            $timeReq['time'] = time();
            $deviceService->dxIotDoor($doorReq);
            $deviceService->dxIotTime($timeReq);
        }

        $cmdModel->insert(['user_id' => $user['id'], 'cmd' => json_encode($req), 'order_no' => $order_no,
            'create_time' => time(), 'shop_id' => $clean['shop_id'], 'room_id' => $clean['room_id']]);


        $this->success("");
    }

}
